﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameObjectsAndEquipment;

namespace StateMachine.GameStates
{
    class MainMenuState : GameState
    {
        private Sprite logo;
        private SpriteFont font;

        private List<TextObject> menuEntries;

        public MainMenuState(int screenWidth, int screenHeight)
            :base(screenWidth, screenHeight)
        {
            menuEntries = new List<TextObject>();
        }

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            base.LoadContent(content);
            font = content.Load<SpriteFont>("basic");

            logo = new Sprite(new Vector2(0, 0), content.Load<Texture2D>("logo"));
            logo.X = screenWidth / 2 - logo.Width / 2;
            logo.Y = 20;


            TextObject newGameEntry = new TextObject(new Vector2(0,0),"New Game", font, Color.Red);
            newGameEntry.X = screenWidth / 2 - newGameEntry.Width / 2;
            newGameEntry.Y = screenHeight / 2;

            TextObject exitEntry = new TextObject(new Vector2(0, 0), "Exit", font, Color.Red);
            exitEntry.X = screenWidth / 2 - exitEntry.Width / 2;
            exitEntry.Y = newGameEntry.Y + 30;

            menuEntries.Add(newGameEntry);
            menuEntries.Add(exitEntry);


        }

        public override void HandleInput(GameStateManager manager, Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.HandleInput(manager, gameTime);

            MouseState mouseState = Mouse.GetState();

            //check if mouse is over a menu entry
            for (int i = 0; i < menuEntries.Count; i++)
            {
                if (CheckIfMouseOver(menuEntries[i], mouseState))
                {
                    menuEntries[i].Color = Color.Yellow;
                    if (mouseState.LeftButton == ButtonState.Pressed)
                    {
                        SolveMenuClick(i, manager);
                    }
                }
                else
                {
                    menuEntries[i].Color = Color.Red;
                }
            }


        }

        public override void Update(GameStateManager manager, Microsoft.Xna.Framework.GameTime gameTime)
        {
            HandleInput(manager, gameTime);

            base.Update(manager, gameTime);

   
        }

        public override void Draw(GameStateManager manager, Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.Draw(manager, gameTime); 
            
            manager.Renderer.ClearScreen(Color.Black);

            manager.Renderer.DrawSprite(logo);

            //Draw menu entries

            for (int i = 0; i < menuEntries.Count; i++)
            {
                manager.Renderer.DrawText(menuEntries[i],font);
            }
           
        }

        private void SolveMenuClick(int index, GameStateManager manager)
        {
            switch (index)
            {
                case 0:
                    manager.ChangeState(new GameplayState(screenWidth, screenHeight));
                    break;
                case 1:
                    manager.ExitGame();
                    break;
                default:
                    break;
            }
        }

        private Boolean CheckIfMouseOver(TextObject textObject, MouseState mouseState)
        {
            if (mouseState.X > textObject.X && mouseState.X < textObject.X + textObject.Width)
            {
                if (mouseState.Y > textObject.Y && mouseState.Y < textObject.Y + textObject.Height)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
